#!/bin/bash

# Licensa GPL-3
# 
# DEBIAN LIKE
#
# ESPECIFICO para processadores AMD = K8, K10, K10.5
#
# NECESSARIO versão GCC-4.7.3 ou superior
#
# Tamanho final da pasta /usr/src/linux-3.4.xx = 9.6 GB
#
# Tempo de compilação ~150 min.
#
###########################################################

#versão do kernel a ser compilado
kernel=3.12

#path de atualização
patchkernel=3.12.22

#patch BFQ
#bfq=3.4.0-v4

#patch do kernel ubuntu: 0001... 0002... 0003... 0004... site: http://kernel.ubuntu.com/~kernel-ppa/mainline/
P1=0001-base-packaging.patch
P2=0002-debian-changelog.patch
P3=0003-configs-based-on-Ubuntu-3.12.0-2.7.patch
#P4=0004-configs-based-on-Ubuntu-3.4.0-5.11.patch 


#apelido do kernel ubuntu
apelido=trusty

#patch ck
#patchck=3.4-ck3

#sufixo
ckk=amd


#.config antigo
kernelantigo=atual

############################################

# não precisa mexer em mais nada aqui

##################################
#arquitetura: amd64 ou i386 # criado por Stivekx - forum ubuntu-br #######
arqt=$(dpkg --print-architecture)


#CL=CONCURRENCY_LEVEL do processador # criado por Stivekx  ############
CL=$(grep -c processor /proc/cpuinfo)
##################################

########baixando compiladores e dependencias########################################################
sudo apt-get update
sudo apt-get install kernel-package libncurses5 libncurses5-dev build-essential patch gcc make gzip bzip2 lzip lzma lzop lrzip lrzsz xz-utils -y
sudo apt-get install gcc-4.7 -y
####################################################################################################



######  deletando patchs antigos  #############
#sudo rm /usr/src/patch-$patchkernel
#sudo rm /usr/src/patch-$patchck
#sudo rm /usr/src/*.patch
#sudo rm /usr/src/0002-debian-changelog.patch
#sudo rm /usr/src/0003-default-configs.patch
#sudo rm /usr/src/0001-block-cgroups-kconfig-build-bits-for-BFQ-v4-$kernel.patch
#sudo rm /usr/src/0002-block-introduce-the-BFQ-v4-I-O-sched-for-$kernel.patch
############################################

#######
sudo mkdir /usr/src/bkpatch
sudo mv /usr/src/*.patch /usr/src/bkpatch
#######

cd /usr/src
wget -c http://www.linux-libre.fsfla.org/pub/linux-libre/releases/$kernel-gnu/linux-libre-$kernel-gnu.tar.bz2
#wget -c http://www.kernel.org/pub/linux/kernel/v3.x/linux-$kernel.tar.bz2


sudo tar -jxpvf /usr/src/linux-libre-$kernel-gnu.tar.bz2
sudo mv /usr/src/linux-$kernel /usr/src/linux-libre-$patchkernel-$ckk


wget -c http://www.linux-libre.fsfla.org/pub/linux-libre/releases/$patchkernel-gnu/patch-$kernel-gnu-$patchkernel-gnu.xz
#wget -c http://www.kernel.org/pub/linux/kernel/v3.x/patch-$patchkernel.xzy
#wget -c http://ck.kolivas.org/patches/3.0/$kernel/$patchck/patch-$patchck.bz2
#wget -c http://algo.ing.unimo.it/people/paolo/disk_sched/patches/$bfq/0001-block-cgroups-kconfig-build-bits-for-BFQ-v4-$kernel.patch
#wget -c http://algo.ing.unimo.it/people/paolo/disk_sched/patches/$bfq/0002-block-introduce-the-BFQ-v4-I-O-sched-for-$kernel.patch



#ubuntu patch
wget -c http://kernel.ubuntu.com/~kernel-ppa/mainline/v$patchkernel-$apelido/$P1
wget -c http://kernel.ubuntu.com/~kernel-ppa/mainline/v$patchkernel-$apelido/$P2
wget -c http://kernel.ubuntu.com/~kernel-ppa/mainline/v$patchkernel-$apelido/$P3
wget -c http://kernel.ubuntu.com/~kernel-ppa/mainline/v$patchkernel-$apelido/$P4




sudo xz -d /usr/src/patch-$kernel-gnu-$patchkernel-gnu.xz
#sudo bunzip2 /usr/src/patch-libre-$patchck.bz2


cd /usr/src/linux-libre-$patchkernel-$ckk

sudo patch -p1 < /usr/src/patch-$kernel-gnu-$patchkernel-gnu
#sudo patch -p1 < /usr/src/patch-$patchck
#sudo patch -p1 < /usr/src/0001-block-cgroups-kconfig-build-bits-for-BFQ-v4-$kernel.patch
#sudo patch -p1 < /usr/src/0002-block-introduce-the-BFQ-v4-I-O-sched-for-$kernel.patch
sudo patch -p1 < /usr/src/$P1
sudo patch -p1 < /usr/src/$P2
sudo patch -p1 < /usr/src/$P3
sudo patch -p1 < /usr/src/$P4

######################
sleep 2

sudo rm /boot/download
cd /boot
sudo wget -c http://sourceforge.net/projects/scriptkernel/files/config-kernel-12/download
sudo cp /boot/download /usr/src/linux-libre-$patchkernel-$ckk/.config

#sudo wget -c https://sourceforge.net/p/scriptkernel/tickets/45/attachment/config-3.10.15-amd

#sudo wget -c https://sourceforge.net/p/scriptkernel/tickets/38/attachment/config-3.4.37-amd

#sudo wget -c http://sourceforge.net/p/scriptkernel/tickets/34/attachment/config-3.4_INTEL_P4

#sudo cp /boot/config-3.10.15-amd /usr/src/linux-$patchkernel-$ckk/.config

#sudo cp /boot/config-3.4_INTEL_P4 /usr/src/linux-$patchkernel-$ckk/.config

#### criado por Stivekx #############
#config_file=$(locate /boot/config-* | sort -r | head -1)

#sudo cp $config_file /usr/src/linux-$patchkernel-$ckk

#########################

############# Editando o Makefile ######################

sudo sed -e 's/EXTRAVERSION\([ = ]\+\)/EXTRAVERSION = -amd/g' /usr/src/linux-libre-$patchkernel-$ckk/Makefile  > /usr/src/linux-libre-$patchkernel-$ckk/mk && mv /usr/src/linux-libre-$patchkernel-$ckk/mk /usr/src/linux-libre-$patchkernel-$ckk/Makefile

cd /usr/src/linux-libre-$patchkernel-$ckk

#sudo gedit Makefile  

sleep 3

sudo make menuconfig




######## Retirando Modulos Problematicos  ############

sudo sed 's/CONFIG_RTS5139=m/# CONFIG_RTS5139 is not set/g' /usr/src/linux-libre-$patchkernel-$ckk/.config > /usr/src/linux-libre-$patchkernel-$ckk/aa && mv /usr/src/linux-libre-$patchkernel-$ckk/aa /usr/src/linux-libre-$patchkernel-$ckk/.config

sleep 2

sudo sed 's/CONFIG_IGB=m/# CONFIG_IGB is not set/g' /usr/src/linux-libre-$patchkernel-$ckk/.config > /usr/src/linux-libre-$patchkernel-$ckk/bb && mv /usr/src/linux-libre-$patchkernel-$ckk/bb /usr/src/linux-libre-$patchkernel-$ckk/.config

sleep 2

sudo sed 's/CONFIG_IGB_DCA=y/# CONFIG_IGB_DCA is not set/g' /usr/src/linux-libre-$patchkernel-$ckk/.config > /usr/src/linux-libre-$patchkernel-$ckk/cc && mv /usr/src/linux-libre-$patchkernel-$ckk/cc /usr/src/linux-libre-$patchkernel-$ckk/.config

sleep 2

sudo sed 's/CONFIG_IGBVF=m/# CONFIG_IGBVF is not set/g' /usr/src/linux-libre-$patchkernel-$ckk/.config > /usr/src/linux-libre-$patchkernel-$ckk/dd && mv /usr/src/linux-libre-$patchkernel-$ckk/dd /usr/src/linux-libre-$patchkernel-$ckk/.config

sleep 2

sudo sed 's/CONFIG_E1000E=m/# CONFIG_E1000E is not set/g' /usr/src/linux-libre-$patchkernel-$ckk/.config > /usr/src/linux-libre-$patchkernel-$ckk/ee && mv /usr/src/linux-libre-$patchkernel-$ckk/ee /usr/src/linux-libre-$patchkernel-$ckk/.config




sleep 3

######################################################


############ script criado por Stivekx #######################

#!/bin/bash

#Script para automatizar a mudança das flags na hora de compilar o kernel.
#Mais informações sobre como compilar o kernel para seu processador:
#http://ubuntuforum-br.org/index.php/topic,81718.0.html

#Modo de usar:
#Após baixar o kernel, descompactar, criar um link simbólico para ele no /usr/src/linux:
# cd /usr/src/linux	
# sudo su
# chmod +x script.sh
# ./script.sh
#O script deve ser executado como root. Recomendo que use sudo su - e rode o script ao invés de sudo sh script.sh


#Script criado por André Steinn

#Aqui é a lista de arquivos que ele vai dar replace no -march= por -march=native. Você pode mudar essa lista de arquivos passando a lista pela opção a e separados pro virgula
#e.g.: ./script.sh -a arch/x86/boot/compressed/Makefile,arch/x86/boot/Makefile,arquivo1,arquivo2,arquivo3

arquivos=( "arch/x86/boot/compressed/Makefile" "arch/x86/boot/Makefile" "arch/x86/kernel/acpi/realmode/Makefile" "arch/x86/Kconfig.cpu" "arch/x86/Makefile" "arch/x86/Makefile_32.cpu")

#Aqui eu verifico se a pessoa passou algum argumento na hora de executar o script
while getopts ":a:c" opt; do
  case $opt in
    a)
      echo "Você setou uma lista de arquivos personalizada: $OPTARG" >&2
	IFS=',' read -ra arquivos <<< "$OPTARG"
      ;;
    \?)
      echo "Opção inválida: -$OPTARG" >&2
      exit 1
      ;;
  esac
done

#Aqui eu faço um loop e altero os arquivos, removendo o -march=() por -march=native
for i in "${arquivos[@]}"
do
	echo "Path no arquivo:"$i
	sed -e 's/-march=\([A-Z0-9a-z]\+\)/-march=native -Ofast /g'  $i > "$i.file_changed"
	mv "$i.file_changed" $i
done



################

sleep 3


#time sudo CONCURRENCY_LEVEL=2 make-kpkg --initrd kernel_image kernel_headers modules_image

#time sudo CONCURRENCY_LEVEL=$CL make-kpkg --initrd kernel_image kernel_headers modules_image

#cd /usr/src
#sudo dpkg -i linux-image-$patchkernel-$ckk\_$patchkernel-$ckk-10.00.Custom_$arqt.deb linux-headers-$patchkernel-$ckk\_$patchkernel-$ckk-10.00.Custom_$arqt.deb

cd /usr/src/linux-libre-$patchkernel-$ckk

time sudo make -j2 
time sudo make modules_install
time sudo make install


sleep 3

cd /lib/modules
ls

#sudo mkinitramfs -o /boot/initrd.img-$patchkernel-$ckk $patchkernel-$ckk
sudo update-grub

sleep 5
echo 'tudo belezinha'
